﻿using System.Threading.Tasks;

namespace Sidvall.Net
{
    public class ProxyStrategy : IProxyStrategy
    {
        #region Public Members

        #region Proxy

        public virtual ProxySettings Proxy
        {
            get
            {
                return Sidvall.Net.HttpSystemContext.Current.Proxy;
            }
        }

        #endregion
        #region ProxyFilePath

        protected virtual string ProxyFilePath
        {
            get
            {
                return "proxy.json";
            }
        }

        #endregion
        #region ProxyIsLoaded

        private bool _ProxyIsLoaded;
        public virtual bool ProxyIsLoaded
        {
            get
            {
                return _ProxyIsLoaded;
            }
            protected set
            {
                _ProxyIsLoaded = value;
            }
        }

        #endregion

        #region LoadProxy

        public virtual void LoadProxy(string content)
        {
            var tokenItem = Sidvall.Serialization.SerializerManager.Current.TryDeserialize<ProxySettings>(content, () =>
            {
                return null;
            });
            LoadProxy(tokenItem);
        }
        public virtual void LoadProxy(ProxySettings proxy)
        {
            Sidvall.Net.HttpSystemContext.Current.InitializeProxy(proxy);
        }

        #endregion
        #region LoadProxyAsync

        public virtual async Task LoadProxyAsync(bool refresh)
        {
            if (!this.ProxyIsLoaded)
            {
                var content = await ReadTokenFileAsync().ConfigureAwait(false);
                LoadProxy(content);
                this.ProxyIsLoaded = true;
            }
            else if (refresh)
            {
                var content = await ReadTokenFileAsync().ConfigureAwait(false);
                LoadProxy(content);
            }
        }

        #endregion
        #region ReadTokenFileAsync

        protected virtual async Task<string> ReadTokenFileAsync()
        {
            if (Sidvall.SystemContext.Current.UserStorageManager != null)
            {
                try
                {
                    if (await Sidvall.SystemContext.Current.UserStorageManager.FileExistsAsync(this.ProxyFilePath).ConfigureAwait(false))
                        return await Sidvall.SystemContext.Current.UserStorageManager.ReadAllTextAsync(this.ProxyFilePath).ConfigureAwait(false);
                }
                catch (System.Exception)
                {
                    return null;
                }
            }
            return null;
        }

        #endregion
        #region SaveProxyAsync

        public virtual async Task SaveProxyAsync()
        {
            if (this.Proxy != null)
            {
                var content = Sidvall.Serialization.SerializerManager.Current.SerializeObject(this.Proxy);
                await Sidvall.SystemContext.Current.UserStorageManager.WriteAllTextAsync(this.ProxyFilePath, content).ConfigureAwait(false);
            }
            else
            {
                if (await Sidvall.SystemContext.Current.UserStorageManager.FileExistsAsync(this.ProxyFilePath).ConfigureAwait(false))
                    await Sidvall.SystemContext.Current.UserStorageManager.DeleteFileAsync(this.ProxyFilePath).ConfigureAwait(false);
            }
        }

        #endregion

        #endregion
        #region Constructors

        public ProxyStrategy()
        {
        }

        #endregion
    }
}
